//
//  homework.swift
//  foundationDemo
//
//  Created by Zhao Mingzhi on 16/6/21.
//  Copyright © 2016年 HNA. All rights reserved.
//

import Foundation
import UIKit

func connection(){
    let urlString:String="http://www.weather.com.cn/adat/cityinfo/101010100.html"
    let url:NSURL! = NSURL(string:urlString)
    
    let request:NSURLRequest = NSURLRequest(URL: url)
    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithRequest(request,
        completionHandler:{(data, response, error) -> Void in
            guard let _data = data! as? NSData else{
                // ...
                return
            }
            let json:AnyObject! = try?NSJSONSerialization.JSONObjectWithData(_data, options:NSJSONReadingOptions.AllowFragments )
            print(json)
            
//            NSKeyedArchiver.archiveRootObject(json, toFile: String)
            
//            let string = String(data: _data, encoding: NSUTF8StringEncoding)
//            print(string)
            
            
//            let document = getDocument();
//            let path:NSString = document.stringByAppendingPathComponent("weather.txt")
//            let res:Bool = NSKeyedArchiver.archiveRootObject(string!, toFile: path as String)
//            if(res){
//                print("文档归档成功")
//                do {
//                    let res1 =  NSData(contentsOfFile: path, : <#T##NSDataReadingOptions#>)
//                    print(res1)
//                }catch let error as NSError{
//                    print(error)
//                }
//                
//            }
        }) as NSURLSessionTask
    dataTask.resume()
}